<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreCoupon;
use App\Http\Requests\UpdateCoupon;
use App\Models\Coupon;
use App\Models\CouponType;
use App\Models\Store;
use App\Models\Type;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CouponController extends Controller
{


    public function __construct()
    {
        view()->share([
            '_cs' => 'am-in',
            '_coupon' => 'am-active',
        ]);

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //优惠券管理首页
    public function index()
    {
        //所有优惠券，关联门店，关联使用类型
        $coupons = Coupon::with('store')->with('types')->paginate(15);

        return view('admin.coupon.index', compact('coupons'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //优惠券新增页面
    public function create()
    {
        //所有门店
        $cates = Store::all();
        //所有使用类型
        $types = Type::all();
        return view('admin.coupon.create', compact('cates', 'types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    //执行新增
    public function store(StoreCoupon $request)
    {
        //除了type_id的全部字段
        $coupon = $request->except('type_id');
        //字符串转数组
        $number = explode(".", $request->ratio);
        //保证小数位最多一位
        if (isset($number[1]) && strlen($number[1]) > 1) {
            return back()->with('success', '提成百分比最多一位小数');
        }
        $time = explode(" ~ ", $request->input('date'));
        $coupon['start_at'] = $time[0];
        $coupon['end_at'] = $time[1];
        $coupon = Coupon::create($coupon);
        //关联使用类型，同步更新
        $coupon->types()->sync($request->type_id);

        return redirect('/admin/coupon')->with('success', '新增成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //所有门店
        $cates = Store::all();
        //所有适用类型
        $types = Type::all();
        //当前优惠券
        $coupon = Coupon::with('store')->find($id);
        // 当前优惠券包含的type_id
        $type_id = CouponType::where('coupon_id', $id)->pluck('type_id')->toarray();


        return view('admin.coupon.edit', compact('coupon', 'cates', 'types', 'type_id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //执行修改
    public function update(UpdateCoupon $request, $id)
    {
        //除了type_id之外的全部字段
        $data = $request->except('type_id');
        //字符串转数组
        $number = explode(".", $request->ratio);
        //保证最多只能一位小数
        if (isset($number[1]) && strlen($number[1]) > 1) {
            return back()->with('success', '提成百分比最多一位小数');
        }

        $time = explode("~", $request->input('date'));

        $data['start_at'] = $time[0];
        $data['end_at'] = $time[1];
        //当前优惠券，关联门店
        $coupon = Coupon::with('store')->find($id);
        $coupon->update($data);
        //关联适用类型同步更新
        $coupon->types()->sync($request->type_id);

        return redirect('/admin/coupon')->with('success', '修改成功~');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //删除优惠券
    public function destroy($id)
    {
        //需判断时候时候有用户拥有改优惠券且没有过期
        $coupon = Coupon::where('id', $id)->withCount(['oncustomers' => function ($query) {
            $query->where('is_can',1);
        }])->first();

        if (strtotime($coupon->end_at) - time() > 0 && $coupon->oncustomers_count > 0) {
            return back()->with('success', '该优惠劵有客人拥有且未过期不能删除');
        } else {
            Coupon::destroy($id);
            CouponType::where('coupon_id', $id)->delete();
            return back()->with('success', '删除成功');
        }

    }

      //是否使用
    public function change_attr(Request $request)
    {
       //当前优惠券信息
        $coupon = Coupon::find($request->id);

        if ($coupon->is_use == 0) {
            $data['is_use'] = 1;
            $coupon->update($data);
        } else {
            $data['is_use'] = 0;
            $coupon->update($data);
        }
    }
       //Ajax排序
    public function sort(Request $request)
    {
        //当前优惠券
        $coupon = Coupon::find($request->id);
        //更新sort字段
        $coupon->sort = $request->sort;
        $coupon->save();
    }


}
